/*******************************************************************************
  Copyright(c) 2009 Geoffrey Hausheer. All rights reserved.

  This program is free software; you can redistribute it and/or modify it
  under the terms of the GNU General Public License as published by the Free
  Software Foundation; either version 2 of the License, or (at your option)
  any later version.

  This program is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  more details.

  You should have received a copy of the GNU General Public License along with
  this program; if not, write to the Free Software Foundation, Inc., 59
  Temple Place - Suite 330, Boston, MA  02111-1307, USA.

  The full GNU General Public License is included in this distribution in the
  file called LICENSE.

  Contact Information: dso-tonight-at-phracturedblue-dot-com
*******************************************************************************/

package dsotonight;

import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Hashtable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author ghaushe
 */
public class SkyDBTableModel extends DefaultTableModel {
    private Hashtable namesHash;
    public void createColumns() {
            addColumn("Name");
            addColumn("Type");
            addColumn("RA");
            addColumn("Dec");
            addColumn("Constellation");
            addColumn("VMag");
            addColumn("SB");
            addColumn("MaxSize");
            addColumn("MinSize");
    }
    public void loadData() {
        try {
            URL url = this.getClass().getResource("/dsotonight/resources/ngcic.csv");
            loadDataFromCSV(url);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private double toDouble(String str[], int index) {
        if (str.length < index + 1) {
            return (double) 0.0;
        }
        return str[index].length() == 0 ? (float) 0.0 : Double.valueOf(str[index]);
    }

    public static String constellationID(String str) {
        return str;
    }

    public static String constellationExpand(String str) {
        return str;
    }

    public static String typeToString(int type) {
        switch (type) {
            case 1:
                return "Galaxy";
            case 2:
                return "Galactic Nebula";
            case 3:
                return "Planetary Nebula";
            case 4:
                return "Open Cluster";
            case 5:
                return "Globular Cluster";
            case 6:
                return "Part of Galaxy";
            case 7:
                return "NGC Duplicate";
            case 8:
                return "IC Duplicate";
            case 9:
                return "Star";
        }
        return "Unknown";
    }

    public ArrayList<String> getNames(String catalog) {
        String names = (String)namesHash.get(catalog);
        if (names == null) {
            return new ArrayList<String>();
        }
        return new ArrayList<String>(Arrays.asList(names.split(";")));
    }
    
    public Hashtable getHash() {
        return namesHash;
    }

    private void loadDataFromCSV(URL url) {
        namesHash = new Hashtable();
        try {
            LineNumberReader lnr = new LineNumberReader(new InputStreamReader(url.openStream()));
            String line;
            while ((line = lnr.readLine()) != null) {
                Object row[];
                String elems[] = line.split("\t");
                String catalog = (elems[0].equals("N") ? "NGC " : "IC ") + elems[1] + elems[2] + (elems[3].length() == 0 ? "" : ("_" + elems[2]));
                int status = Integer.valueOf(elems[5]);
                String constellation = elems[7];
                double RA = Double.valueOf(elems[8]) + Double.valueOf(elems[9]) / 60 + Double.valueOf(elems[10]) / 3600;
                double dec = (elems[11].equals("-") ? -1.0 : 1.0) * Double.valueOf(elems[12]) + Double.valueOf(elems[13]) / 60 + Double.valueOf(elems[14]) / 3600;
                double vmag = toDouble(elems, 16);
                double sb = toDouble(elems, 17);
                double max_size = toDouble(elems, 18);
                double min_size = toDouble(elems, 19);
                String names = catalog;
                if (elems.length > 22) {
                    if (elems[22].length() != 0) {
                        names += ";PGC " + (elems[22]);
                    }
                    for (int i = 23; i < elems.length; i++) {
                        names += ";" + elems[i];
                    }
                }
                namesHash.put(catalog, names);
                row = new Object[]{
                            catalog,
                            status,
                            RA,
                            dec,
                            constellationExpand(constellation),
                            vmag,
                            sb,
                            max_size,
                            min_size};
                addRow(row);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
